package com.example.qxfw.nyqx.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.qxfw.common.entity.ny.CropGrowdateFcst;
import com.example.qxfw.nyqx.mapper.CropGrowdateFcstMapper;
import com.example.qxfw.nyqx.service.CropGrowdateFcstService;
import com.example.qxfw.nyqx.vo.GrowdateFcstListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class CropGrowdateFcstServiceImpl implements CropGrowdateFcstService {

    @Autowired
    private CropGrowdateFcstMapper cropGrowdateFcstMapper;

    @Override
    public List<GrowdateFcstListVo> listByYear(long year1, long year2, String stationId, String type, String cropName, String growDateName) {
        return cropGrowdateFcstMapper.selectList2(year1, year2, stationId, type, cropName, growDateName);
    }

    @Override
    public List<CropGrowdateFcst> listByYear1(long year, String stationId, String type, String cropName) {
        return cropGrowdateFcstMapper.selectList(Wrappers.<CropGrowdateFcst>lambdaQuery()
                .eq(CropGrowdateFcst::getYear, year)
                .eq(CropGrowdateFcst::getStationid, stationId)
                .eq(CropGrowdateFcst::getCropname, cropName)
                .eq(CropGrowdateFcst::getType, type));
    }

    @Override
    public List<CropGrowdateFcst> listByYear2(long year1, long year2, String stationId, String type, String cropName, String growName) {
        return cropGrowdateFcstMapper.selectList(Wrappers.<CropGrowdateFcst>lambdaQuery()
                .ge(CropGrowdateFcst::getYear,year1)
                .le(CropGrowdateFcst::getYear,year2)
                .eq(CropGrowdateFcst::getStationid,stationId)
                .eq(CropGrowdateFcst::getType,type)
                .eq(CropGrowdateFcst::getCropname,cropName)
                .eq(!growName.equals("全部发育期"),CropGrowdateFcst::getGrowdatename,growName));
    }

    @Override
    public List<String> getForecastDate(String year, String stationId, String type, String cropName, String growDateName) {
        return cropGrowdateFcstMapper.selectForecastDate(year, stationId, type, cropName, growDateName);
    }

    @Override
    public List<String> getForecastDate1(String year, String stationId, String type, String cropName) {
        return cropGrowdateFcstMapper.selectForecastDate1(year, stationId, type, cropName);
    }

    @Override
    public List<String> getForecastDate2(String year, String stationId, String type, String growDateName) {
        return cropGrowdateFcstMapper.selectForecastDate2(year, stationId, type, growDateName);
    }

    @Override
    public List<CropGrowdateFcst> listByParam(String year, String stationId, String type, String cropName, String growDateName) {
        return cropGrowdateFcstMapper.selectList(Wrappers.<CropGrowdateFcst>lambdaQuery()
                .eq(CropGrowdateFcst::getYear, Long.parseLong(year))
                .eq(CropGrowdateFcst::getStationid, stationId)
                .eq(type != null, CropGrowdateFcst::getType, type)
                .eq(CropGrowdateFcst::getCropname, cropName)
                .eq(CropGrowdateFcst::getGrowdatename, growDateName));
    }

    @Override
    public List<CropGrowdateFcst> getList2(String year, String cropName, String type, String growDateName) {
        return cropGrowdateFcstMapper.selectList1(year, cropName, type, growDateName);
    }

    @Override
    public List<CropGrowdateFcst> getList3(long year, String cropName, String type, String growDateName) {
        return cropGrowdateFcstMapper.selectList3(year, cropName, type, growDateName);
    }

    @Override
    public Integer update(CropGrowdateFcst cropGrowdateFcst) {
        CropGrowdateFcst cropGrowdateFcst1 = new CropGrowdateFcst();
        cropGrowdateFcst1.setForecastdate(cropGrowdateFcst.getForecastdate());
        cropGrowdateFcst1.setYear(cropGrowdateFcst.getYear());

        return cropGrowdateFcstMapper.update(cropGrowdateFcst1, Wrappers.<CropGrowdateFcst>lambdaUpdate()
                .eq(CropGrowdateFcst::getYear, cropGrowdateFcst.getYear())
                .eq(CropGrowdateFcst::getStationid, cropGrowdateFcst.getStationid())
                .eq(cropGrowdateFcst.getType() != null, CropGrowdateFcst::getType, cropGrowdateFcst.getType())
                .eq(CropGrowdateFcst::getCropname, cropGrowdateFcst.getCropname())
                .eq(CropGrowdateFcst::getGrowdatename, cropGrowdateFcst.getGrowdatename()));
    }

    @Override
    public Integer save(CropGrowdateFcst cropGrowdateFcst) {
        return cropGrowdateFcstMapper.insert(cropGrowdateFcst);
    }
}
